package com.ks.controller;

import com.ks.base.BaseAction;
import com.ks.entity.*;
import com.ks.service.*;
import com.ks.util.HtmlUtil;
import com.ks.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统级controller
 */
@Controller
@RequestMapping("/sys")
public class SysController extends BaseAction {
    private final static Logger log = Logger.getLogger(SysController.class);

    @Autowired(required = false)
    private UserService<User> userService;
    @Autowired(required = false)
    private RoleService<Role> roleService;
    @Autowired(required = false)
    private UnitService<Unit> unitService;
    @Autowired(required = false)
    private ExamPaperService<ExamPaper> examPaperService;
    @Autowired(required = false)
    private DepositoryService<Depository> depositoryService;
    @Autowired(required = false)
    private ExamineeService<Examinee> examineeService;
    /**
     * 登录跳转
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/login")
    public ModelAndView log(HttpServletRequest request, HttpServletResponse response) throws Exception {

        System.out.println("登录页面跳转");
        return forword("pages/login", null);
    }

    /**
     * 登录
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/proof")
    @ResponseBody
    public void login(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        System.out.println("执行登录操作");
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        Map<String, Object> jsonMap = new HashMap<>();

        jsonMap = userService.login(request, account, password);
        // 设置页面数据
        HtmlUtil.writerJson(response, jsonMap);
    }

    @RequestMapping("/index")
    // 此处为记录AOP拦截Controller记录用户操作 管理员入口
    public ModelAndView toIndex(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession(false);
        Map<String, Object> context = getRootMap();
        context.put("role", "admin");
        if (null != session && null != session.getAttribute("userInfo")) {
            Role role = roleService.queryById(1);
            request.getSession().setAttribute("role", "admin");
            request.getSession().setAttribute("name", role.getName());
            return forword("pages/roleMng/role-list", context);
        } else {
            return forword("redirect:login", context);
        }
    }

    @RequestMapping("/master")
    // 此处为记录AOP拦截Controller记录用户操作 工作人员入口
    public ModelAndView toMaster(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession(false);
        Map<String, Object> context = getRootMap();
        context.put("role", "master");
        if (null != session && null != session.getAttribute("userInfo")) {
            Role role = roleService.queryById(2);
            request.getSession().setAttribute("role", "master");
            request.getSession().setAttribute("name", role.getName());
            return forword("pages/typeMng/type", context);
        } else {
            return forword("redirect:login", context);
        }
    }

    @RequestMapping("/trainee")
    // 此处为记录AOP拦截Controller记录用户操作 学员入口
    public ModelAndView toTrainee(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession(false);
        Map<String, Object> context = getRootMap();
        context.put("role", "trainee");
        if (null != session && null != session.getAttribute("userInfo")) {
            User user = (User) session.getAttribute("userInfo");
            Role role = roleService.queryById(3);
            request.getSession().setAttribute("role", "trainee");
            request.getSession().setAttribute("name", role.getName());
          /*  Exam exam = examPaperService.waitingPage(user);
            Map<String, Object> map = new HashMap<>();
            map.put("exam_id",exam.getId());
            map.put("user_id",user.getId());
            boolean bool = false;
            List<Examinee> examinees = examineeService.queryList(map);
            if (examinees.size()==0){
                bool = true;
            } else {
                bool = StringUtils.isBlank(examinees.get(0).getScore());
            }
            if (null != exam && bool) {
                context.put("exam",exam);
                return forword("/pages/examining/exam-start", context);
            } else {*/
                context = depositoryService.praticeIndex(user);
                return forword("/pages/practice/item-knows", context);
            //}
        } else {
            return forword("redirect:login", context);
        }
    }

    /**
     * 销毁session 登出操作
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/logout")
    // 此处为记录AOP拦截Controller记录用户操作 学员入口
    public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession(false);
        session.invalidate();
        return forword("redirect:login", null);

    }

    /**
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/userInfo")
    // 此处为记录AOP拦截Controller记录用户操作 学员入口
    public ModelAndView userInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession(false);
        Map<String, Object> context = getRootMap();
        User user = (User) session.getAttribute("userInfo");
        context.put("user",user);
        context.put("role",roleService.queryListByUserId(user.getId()).get(0));
        context.put("unit",unitService.queryById(user.getUnit_id()));
        return forword("/pages/roleMng/userInfo", context);

    }

}